<?php
/**
 * 书籍管理
 * User: Richard
 * Date: 2020/3/14
 * Time: 16:05
 */

namespace Demo\Controller;


use Common\Controller\DemoBaseController;

class BookController extends DemoBaseController
{

    private $bookModel;

    public function __construct()
    {
        parent::__construct();
        $this->bookModel = M('books');
    }

    /**
     * 书籍列表
     */
    public function index()
    {
        $pageCount = I('count', 20, 'intval');//一页20个

        $where = ['status' => 1];
        $name = I('name');
        if (!empty($name)) {
            $where['name|desc'] = ['like', '%' . $name . '%'];
        }

        $categoryId = I('category_id');
        if (!empty($categoryId)) {
            $where['category_id'] = $categoryId;
        }
        $count = $this->bookModel->where($where)->count();
        $page = $this->page($count, $pageCount);

        $list = $this->bookModel->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order('create_time desc')
            ->select();
        $this->returnok(['list' => $list, 'page' => $page]);
    }

    /**
     * 添加书籍
     */
    public function add()
    {
        $category = M('book_categorys')->where(['status' => 1])->select();
        $this->returnok($category);
    }

    /**
     * 添加书籍提交
     */
    public function add_post()
    {
        $categoryId = I('category_id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '请选择书籍分类');
        }
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍名称');
        }
        $author = I('author');
        if (empty($author)) {
            $this->returnerror('', '请填写作者');
        }
        $desc = I('desc');
        $guide = I('guide');

        $bg = I('bg');
        if (empty($bg)) {
            $this->returnerror('', '请上传书籍封面');
        }
        $audio = I('audio');

        $data = [
            'category_id' => $categoryId,//书籍分类
            'name' => $name,//书籍名称
            'author' => $author,//作者
            'desc' => $desc,//书籍描述
            'guide' => $guide,//导读
            'bg' => $bg,//封面图片
            'audio' => $audio,//音频
        ];
        $id = $this->bookModel->add($data);
        if ($id) {
            $this->returnok(['id' => $id], '添加成功');
        } else {
            $this->returnerror('', '添加失败，请重试');
        }
    }

    /**
     * 编辑书籍
     */
    public function edit()
    {
        $bookId = I('id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $bookInfo = $this->bookModel->where(['id' => $bookId, 'status' => 1])->find();
        if (empty($bookInfo)) {
            $this->returnerror('', '未找到书籍或书籍已被管理员删除');
        }
        $category = M('book_categorys')->where(['status' => 1])->select();
        $this->returnok(['category' => $category, 'book' => $bookInfo]);
    }

    /**
     * edit书籍提交
     */
    public function edit_post()
    {
        $bookId = I('id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $categoryId = I('category_id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '请选择书籍分类');
        }
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍名称');
        }
        $author = I('author');
        if (empty($author)) {
            $this->returnerror('', '请填写作者');
        }
        $desc = I('desc');
        $guide = I('guide');

        $bg = I('bg');
        if (empty($bg)) {
            $this->returnerror('', '请上传书籍封面');
        }
        $audio = I('audio');

        $data = [
            'category_id' => $categoryId,//书籍分类
            'name' => $name,//书籍名称
            'author' => $author,//作者
            'desc' => $desc,//书籍描述
            'guide' => $guide,//导读
            'bg' => $bg,//封面图片
            'audio' => $audio,//音频
        ];
        $res = $this->bookModel->where(['id' => $bookId])->save($data);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $bookId], '修改成功');
        } else {
            $this->returnerror('', '修改失败，请重试');
        }
    }

    /**
     * 书籍软删除
     */
    public function softDelete()
    {
        $bookId = I('id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookModel->where(['id' => $bookId])->save(['status' => 0]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $bookId], '删除成功');
        } else {
            $this->returnerror('', '删除失败，请重试');
        }
    }

    /**
     * 软删除书籍恢复
     */
    public function reply()
    {
        $bookId = I('id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookModel->where(['id' => $bookId])->save(['status' => 1]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $bookId], '恢复成功');
        } else {
            $this->returnerror('', '恢复失败，请重试');
        }
    }

}